home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / clx.l < prev    next >
Text File  |  1988-09-12  |  31KB  |  944 lines

  1. ;;; -*- Mode: LISP; Syntax: Common-lisp; Package: XLIB; Base: 10; Lowercase: Yes -*-
  2.  
  3. ;; Version 4
  4.  
  5. ;;;
  6. ;;;             TEXAS INSTRUMENTS INCORPORATED
  7. ;;;                  P.O. BOX 2909
  8. ;;;                   AUSTIN, TEXAS 78769
  9. ;;;
  10. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  11. ;;;
  12. ;;; Permission is granted to any individual or institution to use, copy, modify,
  13. ;;; and distribute this software, provided that this complete copyright and
  14. ;;; permission notice is maintained, intact, in all copies and supporting
  15. ;;; documentation.
  16. ;;;
  17. ;;; Texas Instruments Incorporated provides this software "as is" without
  18. ;;; express or implied warranty.
  19. ;;;
  20.  
  21. ;; Primary Interface Author:
  22. ;;    Robert W. Scheifler
  23. ;;    MIT Laboratory for Computer Science
  24. ;;    545 Technology Square, Room 418
  25. ;;    Cambridge, MA 02139
  26. ;;    rws@zermatt.lcs.mit.edu
  27.  
  28. ;; Design Contributors:
  29. ;;    Dan Cerys, Texas Instruments
  30. ;;    Scott Fahlman, CMU
  31. ;;    Kerry Kimbrough, Texas Instruments
  32. ;;    Chris Lindblad, MIT
  33. ;;    Rob MacLachlan, CMU
  34. ;;    Mike McMahon, Symbolics
  35. ;;    David Moon, Symbolics
  36. ;;    LaMott Oren, Texas Instruments
  37. ;;    Daniel Weinreb, Symbolics
  38. ;;    John Wroclawski, MIT
  39. ;;    Richard Zippel, Symbolics
  40.  
  41. ;; Primary Implementation Author:
  42. ;;    LaMott Oren, Texas Instruments
  43.  
  44. ;; Implementation Contributors:
  45. ;;    Chris Lindblad, MIT
  46. ;;    Robert Scheifler, MIT
  47.  
  48. ;;;
  49. ;;; Change history:
  50. ;;;
  51. ;;;  Date    Author        Description
  52. ;;; -------------------------------------------------------------------------------------
  53. ;;; 04/07/87    R.Scheifler    Created code stubs
  54. ;;; 04/08/87    L.Oren        Started Implementation
  55. ;;; 05/11/87    L.Oren        Included draft 3 revisions
  56. ;;; 07/07/87    L.Oren        Untested alpha release to MIT
  57. ;;; 07/17/87    L.Oren        Alpha release
  58. ;;; 08/**/87    C.Lindblad    Rewrite of buffer code
  59. ;;; 08/**/87    et al        Various random bug fixes
  60. ;;; 08/**/87    R.Scheifler    General syntactic and portability cleanups
  61. ;;; 08/**/87    R.Scheifler    Rewrite of gcontext caching and shadowing
  62. ;;; 09/02/87    L.Oren        Change events from resource-ids to objects
  63. ;;; 12/24/87    R.Budzianowski    KCL support
  64. ;;; 12/**/87    J.Irwin        ExCL 2.0 support
  65. ;;; 01/20/88    L.Oren        Add server extension mechanisms
  66. ;;; 01/20/88    L.Oren        Only force output when blocking on input
  67. ;;; 01/20/88    L.Oren        Uniform support for :event-window on events
  68. ;;; 01/28/88    L.Oren        Add window manager property functions
  69. ;;; 01/28/88    L.Oren        Add character translation facility
  70. ;;; 02/**/87    J.Irwin        Allegro 2.2 support
  71.  
  72. ;;; This is considered a somewhat changeable interface.  Discussion of better
  73. ;;; integration with CLOS, support for user-specified subclassess of basic
  74. ;;; objects, and the additional functionality to match the C Xlib is still in
  75. ;;; progress.  Bug reports should be addressed to bug-clx@expo.lcs.mit.edu.
  76.  
  77. ;; Note: all of the following is in the package XLIB.
  78.  
  79. (in-package 'xlib :use '(lisp))
  80.  
  81. (pushnew :xlib *features*)
  82.  
  83. (export '(
  84.       card32
  85.       card29
  86.       int32
  87.       card16
  88.       int16
  89.       card8
  90.       int8
  91.       rgb-val
  92.       angle
  93.       mask32
  94.       mask16
  95.       array-index
  96.       pixel
  97.       image-depth
  98.       display
  99.       display-p
  100.       display-display
  101.       display-after-function
  102.       display-protocol-major-version
  103.       display-protocol-minor-version
  104.       display-vendor-name
  105.       display-resource-id-base
  106.       display-resource-id-mask
  107.       display-xid
  108.       display-byte-order
  109.       display-version-number
  110.       display-release-number
  111.       display-max-request-length
  112.       display-squish
  113.       display-default-screen
  114.       display-nscreens
  115.       display-roots
  116.       display-motion-buffer-size
  117.       display-xdefaults
  118.       display-image-lsb-first-p
  119.       display-bitmap-format
  120.       display-pixmap-formats
  121.       display-min-keycode
  122.       display-max-keycode
  123.       display-error-handler
  124.       display-authorization-name
  125.       display-authorization-data
  126.       display-plist
  127.       color
  128.       color-p
  129.       color-red
  130.       color-green
  131.       color-blue
  132.       make-color
  133.       color-rgb
  134.       resource-id
  135.       drawable
  136.       drawable-p
  137.       drawable-equal
  138.       drawable-id
  139.       drawable-display
  140.       drawable-plist
  141.       window
  142.       window-p
  143.       window-equal
  144.       window-id
  145.       window-display
  146.       window-plist
  147.       pixmap
  148.       pixmap-p
  149.       pixmap-equal
  150.       pixmap-id
  151.       pixmap-display
  152.       pixmap-plist
  153.       colormap
  154.       colormap-p
  155.       colormap-equal
  156.       colormap-id
  157.       colormap-display
  158.       cursor
  159.       cursor-p
  160.       cursor-equal
  161.       cursor-id
  162.       cursor-display
  163.       xatom
  164.       stringable
  165.       fontable
  166.       timestamp
  167.       bit-gravity
  168.       win-gravity
  169.       grab-status
  170.       boolean
  171.       alist
  172.       repeat-seq
  173.       point-seq
  174.       seg-seq
  175.       rect-seq
  176.       arc-seq
  177.       gcontext
  178.       gcontext-p
  179.       gcontext-equal
  180.       gcontext-id
  181.       gcontext-display
  182.       gcontext-plist
  183.       event-mask-class
  184.       event-mask
  185.       pointer-event-mask-class
  186.       pointer-event-mask
  187.       device-event-mask-class
  188.       device-event-mask
  189.       modifier-key
  190.       modifier-mask
  191.       state-mask-key
  192.       gcontext-key
  193.       event-key
  194.       error-key
  195.       draw-direction
  196.       boole-constant
  197.       bitmap-format
  198.       bitmap-format-p
  199.       bitmap-format-unit
  200.       bitmap-format-pad
  201.       bitmap-format-lsb-first-p
  202.       pixmap-format
  203.       pixmap-format-p
  204.       pixmap-format-depth
  205.       pixmap-format-bits-per-pixel
  206.       pixmap-format-scanline-pad
  207.       visual-info
  208.       visual-info-p
  209.       visual-info-id
  210.       visual-info-class
  211.       visual-info-red-mask
  212.       visual-info-green-mask
  213.       visual-info-blue-mask
  214.       visual-info-bits-per-rgb
  215.       visual-info-colormap-entries
  216.       visual-info-plist
  217.       screen
  218.       screen-p
  219.       screen-root
  220.       screen-width
  221.       screen-height
  222.       screen-width-in-millimeters
  223.       screen-height-in-millimeters
  224.       screen-depths
  225.       screen-root-depth
  226.       screen-root-visual
  227.       screen-default-colormap
  228.       screen-white-pixel
  229.       screen-black-pixel
  230.       screen-min-installed-maps
  231.       screen-max-installed-maps
  232.       screen-backing-stores
  233.       screen-save-unders-p
  234.       screen-event-mask-at-open
  235.       screen-plist
  236.       font
  237.       font-p
  238.       font-equal
  239.       font-id
  240.       font-display
  241.       font-name
  242.       font-direction
  243.       font-min-char
  244.       font-max-char
  245.       font-min-byte1
  246.       font-max-byte1
  247.       font-min-byte2
  248.       font-max-byte2
  249.       font-all-chars-exist-p
  250.       font-default-char
  251.       font-min-bounds
  252.       font-max-bounds
  253.       font-ascent
  254.       font-descent
  255.       font-properties
  256.       font-property
  257.       font-plist
  258.       char-left-bearing
  259.       max-char-left-bearing
  260.       min-char-left-bearing
  261.       char-right-bearing
  262.       max-char-right-bearing
  263.       min-char-right-bearing
  264.       char-width
  265.       max-char-width
  266.       min-char-width
  267.       char-ascent
  268.       max-char-ascent
  269.       min-char-ascent
  270.       char-descent
  271.       max-char-descent
  272.       min-char-descent
  273.       char-attributes
  274.       max-char-attributes
  275.       min-char-attributes
  276.       make-event-mask
  277.       make-event-keys
  278.       make-state-mask
  279.       make-state-keys
  280.       ))
  281.  
  282. (defparameter *protocol-major-version* 11.)
  283. (defparameter *protocol-minor-version* 0)
  284.  
  285. (defparameter *x-tcp-port* 6000) ;; add display number
  286.  
  287. ; Note: various perversions of the CL type system are used below.
  288. ; Examples: (list elt-type) (sequence elt-type)
  289.  
  290. ;; Note: if you have read the Version 11 protocol document or C Xlib manual, most of
  291. ;; the relationships should be fairly obvious.  We have no intention of writing yet
  292. ;; another moby document for this interface.
  293.  
  294. ;; Types employed: display, window, pixmap, cursor, font, gcontext, colormap, color.
  295. ;; These types are defined solely by a functional interface; we do not specify
  296. ;; whether they are implemented as structures or flavors or ...  Although functions
  297. ;; below are written using DEFUN, this is not an implementation requirement (although
  298. ;; it is a requirement that they be functions as opposed to macros or special forms).
  299. ;; It is unclear whether with-slots in the Common Lisp Object System must work on
  300. ;; them.
  301.  
  302. ;; Windows, pixmaps, cursors, fonts, gcontexts, and colormaps are all represented as
  303. ;; compound objects, rather than as integer resource-ids.  This allows applications
  304. ;; to deal with multiple displays without having an explicit display argument in the
  305. ;; most common functions.  Every function uses the display object indicated by the
  306. ;; first argument that is or contains a display; it is an error if arguments contain
  307. ;; different displays, and predictable results are not guaranteed.
  308.  
  309. ;; Each of window, pixmap, cursor, font, gcontext, and colormap have the following
  310. ;; five functions:
  311.  
  312. ;(defun make-<mumble> (display resource-id)
  313. ;  ;; This function should almost never be called by applications, except in handling
  314. ;  ;; events.  To minimize consing in some implementations, this may use a cache in
  315. ;  ;; the display.  Make-gcontext creates with :cache-p nil.  Make-font creates with
  316. ;  ;; cache-p true.
  317. ;  (declare (type display display)
  318. ;       (type integer resource-id)
  319. ;       (values <mumble>)))
  320.  
  321. ;(defun <mumble>-display (<mumble>)
  322. ;  (declare (type <mumble> <mumble>)
  323. ;       (values display)))
  324.  
  325. ;(defun <mumble>-id (<mumble>)
  326. ;  (declare (type <mumble> <mumble>)
  327. ;       (values integer)))
  328.  
  329. ;(defun <mumble>-equal (<mumble>-1 <mumble>-2)
  330. ;  (declare (type <mumble> <mumble>-1 <mumble>-2)))
  331.  
  332. ;(defun <mumble>-p (<mumble>-1 <mumble>-2)
  333. ;  (declare (type <mumble> <mumble>-1 <mumble>-2)
  334. ;       (values boolean)))
  335.  
  336. (deftype boolean () '(or null (not null)))
  337.  
  338. (deftype card32 () '(unsigned-byte 32))
  339.  
  340. (deftype card29 () '(unsigned-byte 29))
  341.  
  342. (deftype int32 () '(signed-byte 32))
  343.  
  344. (deftype card16 () '(unsigned-byte 16))
  345.  
  346. (deftype int16 () '(signed-byte 16))
  347.  
  348. (deftype card8 () '(unsigned-byte 8))
  349.  
  350. (deftype int8 () '(signed-byte 8))
  351.  
  352. ; Note that we are explicitly using a different rgb representation than what
  353. ; is actually transmitted in the protocol.
  354.  
  355. (deftype rgb-val () '(float 0.0 1.0))
  356.  
  357. ; Note that we are explicitly using a different angle representation than what
  358. ; is actually transmitted in the protocol.
  359.  
  360. (deftype angle () 'number)
  361.  
  362. (deftype mask32 () 'card32)
  363.  
  364. (deftype mask16 () 'card16)
  365.  
  366. (deftype pixel () '(unsigned-byte 32))
  367. (deftype image-depth () '(integer 0 32))
  368.  
  369. (deftype resource-id () 'card29)
  370.  
  371. (deftype keysym () 'card32)
  372.  
  373. ; The following functions are provided by color objects:
  374.  
  375. ; The intention is that IHS and YIQ and CYM interfaces will also exist.
  376. ; Note that we are explicitly using a different spectrum representation
  377. ; than what is actually transmitted in the protocol.
  378.  
  379. (defstruct (color (:constructor make-color-internal (red green blue)))
  380.   (red 0.0 :type rgb-val)
  381.   (green 0.0 :type rgb-val)
  382.   (blue 0.0 :type rgb-val))
  383.  
  384. (defun make-color (&key (red 1.0) (green 1.0) (blue 1.0) &allow-other-keys)
  385.   (declare (type rgb-val red green blue))
  386.   (declare-values color)
  387.   (make-color-internal red green blue))
  388.  
  389. (defun color-rgb (color)
  390.   (declare (type color color))
  391.   (declare-values red green blue)
  392.   (values (color-red color) (color-green color) (color-blue color)))
  393.  
  394. (defstruct bitmap-format
  395.   (unit 8 :type (member 8 16 32))
  396.   (pad 8 :type (member 8 16 32))
  397.   (lsb-first-p nil :type boolean))
  398.  
  399. (defstruct pixmap-format
  400.   (depth 0 :type image-depth)
  401.   (bits-per-pixel 8 :type (member 1 4 8 16 24 32))
  402.   (scanline-pad 8 :type (member 8 16 32)))
  403.  
  404. (defparameter *atom-cache-size* 200)
  405. (defparameter *resource-id-map-size* 500)
  406.  
  407. (defstruct (display (:include buffer)
  408.             (:constructor make-display-internal)
  409.             (:print-function display-print))
  410.   host                        ; Server Host
  411.   (display 0 :type integer)            ; Display number on host
  412.   (after-function nil)                ; Function to call after every request
  413.   (waiting-reply-p nil)                ; non-nil when waiting for a reply
  414.   (input-lock (make-process-lock))        ; Lock over reading from the input stream
  415.   (event-lock (make-process-lock))        ; with-event-queue lock
  416.   (event-queue-lock (make-process-lock))    ; new-events/event-queue lock
  417.   (new-events nil :type list)            ; unprocessed events (a cons of event-queue)
  418.   (event-queue (list nil) :type cons)        ; processed and unprocessed events
  419.   (atom-cache (make-hash-table :test #'eq :size *atom-cache-size*)
  420.           :type hash-table)            ; EQ Hash table relating atoms keywords
  421.                         ; to resource id's
  422.   (font-cache nil)                ;list of font
  423.   (protocol-major-version 0 :type card16)    ; Major version of server's X protocol
  424.   (protocol-minor-version 0 :type card16)    ; minor version of servers X protocol
  425.   (vendor-name "" :type string)            ; vendor of the server hardware
  426.   (resource-id-base 0 :type resource-id)    ; resouce ID base
  427.   (resource-id-mask 0 :type resource-id)    ; resource ID mask bits
  428.   (resource-id-byte nil)            ; resource ID mask field (used with DPB & LDB)
  429.   (resource-id-count 0 :type resource-id)    ; resource ID mask count
  430.                         ; (used for allocating ID's)
  431.   (resource-id-map (make-hash-table :test (resource-id-map-test)
  432.                     :size *resource-id-map-size*)
  433.            :type hash-table)        ; hash table maps resource-id's to
  434.                         ; objects (used in lookup functions)
  435.   (xid 'resourcealloc)                ; allocator function
  436.   (byte-order :lsbfirst)            ; screen byte order, LSBFirst, MSBFirst
  437.   (version-number 11 :type card16)        ; X protocol version number.
  438.   (release-number 0 :type card32)        ; release of the server
  439.   (max-request-length 0 :type card16)        ; maximum number 32 bit words in request
  440.   (squish nil :type boolean)            ; Squish MouseMoved events?
  441.   (default-screen)                ; default screen for operations
  442.   (nscreens 1 :type card8)            ; number of screens on this server
  443.   (roots nil :type list)            ; List of screens
  444.   (motion-buffer-size 0 :type card32)        ; size of motion buffer
  445.   (xdefaults)                    ; contents of defaults from server
  446.   (image-lsb-first-p nil :type boolean)
  447.   (bitmap-format (make-bitmap-format)        ; Screen image info
  448.          :type bitmap-format)
  449.   (pixmap-formats nil :type sequence)        ; list of pixmap formats
  450.   (min-keycode 0 :type card8)            ; minimum key-code
  451.   (max-keycode 0 :type card8)            ; maximum key-code
  452.   (error-handler 'default-error-handler)    ; Error handler function
  453.   (close-down-mode :destroy)              ; Close down mode saved by Set-Close-Down-Mode
  454.   (authorization-name "" :type string)
  455.   (authorization-data "" :type string)
  456.   (last-width nil :type (or null card29))    ; Accumulated width of last string
  457.   (keysym-mapping nil                ; Keysym mapping cached from server
  458.           :type (or null (array * (* *))))
  459.   (modifier-mapping nil :type list)        ; ALIST of (keysym . state-mask) for all modifier keysyms
  460.   (keysym-translation nil :type list)        ; An alist of (keysym object function)
  461.                         ; for display-local keysyms
  462.   (extension-alist nil :type list)        ; extension alist, which has elements:
  463.                         ; (name major-opcode first-event first-error)
  464.   (event-extensions '#() :type vector)        ; Vector mapping X event-codes to event keys
  465.   (performance-info)                ; Hook for gathering performance info
  466.   (trace-history)                ; Hook for debug trace
  467.   (plist)                    ; hook for extension to hang data
  468.   )
  469.  
  470. ;;(deftype drawable () '(or window pixmap))
  471.  
  472. (defstruct drawable
  473.   (id 0 :type resource-id)
  474.   (display nil :type (or null display))
  475.   (plist nil :type list)            ; Extension hook
  476.   )
  477.  
  478. (defstruct (window (:include drawable))
  479.   )
  480.  
  481. (defstruct (pixmap (:include drawable))
  482.   )
  483.  
  484. (defstruct colormap
  485.   (id 0 :type resource-id)
  486.   (display nil :type (or null display))
  487.   )
  488.  
  489. (defstruct cursor
  490.   (id 0 :type resource-id)
  491.   (display nil :type (or null display))
  492.   )
  493.  
  494. ; Atoms are accepted as strings or symbols, and are always returned as keywords.
  495. ; Protocol-level integer atom ids are hidden, using a cache in the display object.
  496.  
  497. (deftype xatom () '(or string symbol))
  498.  
  499. (defconstant *predefined-atoms*
  500.          '#(nil :primary :secondary :arc :atom :bitmap
  501.             :cardinal :colormap :cursor
  502.             :cut_buffer0 :cut_buffer1 :cut_buffer2 :cut_buffer3
  503.             :cut_buffer4 :cut_buffer5 :cut_buffer6 :cut_buffer7
  504.             :drawable :font :integer :pixmap :point :rectangle
  505.             :resource_manager :rgb_color_map :rgb_best_map
  506.             :rgb_blue_map :rgb_default_map
  507.             :rgb_gray_map :rgb_green_map :rgb_red_map :string
  508.             :visualid :window :wm_command :wm_hints
  509.             :wm_client_machine :wm_icon_name :wm_icon_size
  510.             :wm_name :wm_normal_hints :wm_size_hints
  511.             :wm_zoom_hints :min_space :norm_space :max_space
  512.             :end_space :superscript_x :superscript_y
  513.             :subscript_x :subscript_y
  514.             :underline_position :underline_thickness
  515.             :strikeout_ascent :strikeout_descent
  516.             :italic_angle :x_height :quad_width :weight
  517.             :point_size :resolution :copyright :notice
  518.             :font_name :family_name :full_name :cap_height
  519.             :wm_class :wm_transient_for))
  520.  
  521. (deftype stringable () '(or string symbol))
  522.  
  523. (deftype fontable () '(or stringable font))
  524.  
  525. ; Nil stands for CurrentTime.
  526.  
  527. (deftype timestamp () '(or null card32))
  528.  
  529. (defconstant *bit-gravity-vector*
  530.          '#(:forget :north-west :north :north-east :west
  531.         :center :east :south-west :south
  532.         :south-east :static))
  533.  
  534. (deftype bit-gravity ()
  535.   '(member :forget :north-west :north :north-east :west
  536.        :center :east :south-west :south :south-east :static))
  537.  
  538. (defconstant *win-gravity-vector*
  539.          '#(:unmap :north-west :north :north-east :west
  540.         :center :east :south-west :south :south-east
  541.         :static))
  542.  
  543. (deftype win-gravity ()
  544.   '(member :unmap :north-west :north :north-east :west
  545.        :center :east :south-west :south :south-east :static))
  546.  
  547. (deftype grab-status ()
  548.   '(member :success :already-grabbed :invalid-time :not-viewable))
  549.  
  550. ; An association list.
  551.  
  552. (deftype alist (key-type-and-name datum-type-and-name)
  553.   key-type-and-name datum-type-and-name 'list)
  554.  
  555. ; A sequence, containing zero or more repetitions of the given elements,
  556. ; with the elements expressed as (type name).
  557.  
  558. (deftype repeat-seq (&rest elts) elts 'sequence)
  559.  
  560. (deftype point-seq () '(repeat-seq (int16 x) (int16 y)))
  561.  
  562. (deftype seg-seq () '(repeat-seq (int16 x1) (int16 y1) (int16 x2) (int16 y2)))
  563.  
  564. (deftype rect-seq () '(repeat-seq (int16 x) (int16 y) (card16 width) (card16 height)))
  565.  
  566. (deftype arc-seq ()
  567.   '(repeat-seq (int16 x) (int16 y) (card16 width) (card16 height)
  568.            (angle angle1) (angle angle2)))
  569.  
  570. (deftype gcontext-state () 'simple-vector)
  571.  
  572. (defstruct (gcontext (:copier nil))
  573.   ;; The accessors convert to CLX data types.
  574.   (id 0 :type resource-id)
  575.   (display nil :type (or null display))
  576.   (drawable nil :type (or null drawable))
  577.   (cache-p t :type boolean)
  578.   (server-state (allocate-gcontext-state) :type gcontext-state)
  579.   (local-state (allocate-gcontext-state) :type gcontext-state)
  580.   (plist nil :type list)            ; Extension hook
  581.   )
  582.  
  583. (defconstant *event-mask-vector*
  584.          '#(:key-press :key-release :button-press :button-release
  585.         :enter-window :leave-window :pointer-motion :pointer-motion-hint
  586.         :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  587.         :button-5-motion :button-motion :keymap-state :exposure :visibility-change
  588.         :structure-notify :resize-redirect :substructure-notify :substructure-redirect
  589.         :focus-change :property-change :colormap-change :owner-grab-button))
  590.  
  591. (deftype event-mask-class ()
  592.   '(member :key-press :key-release :owner-grab-button :button-press :button-release
  593.        :enter-window :leave-window :pointer-motion :pointer-motion-hint
  594.        :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  595.        :button-5-motion :button-motion :exposure :visibility-change
  596.        :structure-notify :resize-redirect :substructure-notify :substructure-redirect
  597.        :focus-change :property-change :colormap-change :keymap-state))
  598.  
  599. (deftype event-mask ()
  600.   '(or mask32 list)) ;; (OR integer (LIST event-mask-class))
  601.  
  602. (defconstant *pointer-event-mask-vector*
  603.          '#(%error %error :button-press :button-release
  604.         :enter-window :leave-window :pointer-motion :pointer-motion-hint
  605.         :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  606.         :button-5-motion :button-motion :keymap-state))
  607.  
  608. (deftype pointer-event-mask-class ()
  609.   '(member :button-press :button-release
  610.        :enter-window :leave-window :pointer-motion :pointer-motion-hint
  611.        :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  612.        :button-5-motion :button-motion :keymap-state))
  613.  
  614. (deftype pointer-event-mask ()
  615.   '(or mask32 list)) ;;  '(or integer (list pointer-event-mask-class)))
  616.  
  617. (defconstant *device-event-mask-vector*
  618.          '#(:key-press :key-release :button-press :button-release :pointer-motion
  619.         :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  620.         :button-5-motion :button-motion))
  621.  
  622. (deftype device-event-mask-class ()
  623.   '(member :key-press :key-release :button-press :button-release :pointer-motion
  624.        :button-1-motion :button-2-motion :button-3-motion :button-4-motion
  625.        :button-5-motion :button-motion))
  626.  
  627. (deftype device-event-mask ()
  628.   '(or mask32 list)) ;;  '(or integer (list device-event-mask-class)))
  629.  
  630. (defconstant *state-mask-vector*
  631.          '#(:shift :lock :control :mod-1 :mod-2 :mod-3 :mod-4 :mod-5
  632.         :button-1 :button-2 :button-3 :button-4 :button-5))
  633.  
  634. (deftype modifier-key ()
  635.   '(member :shift :lock :control :mod-1 :mod-2 :mod-3 :mod-4 :mod-5))
  636.  
  637. (deftype modifier-mask ()
  638.   '(or (member :any) mask16 list)) ;;  '(or (member :any) integer (list modifier-key)))
  639.  
  640. (deftype state-mask-key ()
  641.   '(or modifier-key (member :button-1 :button-2 :button-3 :button-4 :button-5)))
  642.  
  643. (defconstant *gcontext-components*
  644.          '(:function :plane-mask :foreground :background
  645.            :line-width :line-style :cap-style :join-style :fill-style
  646.            :fill-rule :tile :stipple :ts-x :ts-y :font :subwindow-mode
  647.            :exposures :clip-x :clip-y :clip-mask :dash-offset :dashes
  648.            :arc-mode))
  649.  
  650. (deftype gcontext-key ()
  651.   '(member :function :plane-mask :foreground :background
  652.        :line-width :line-style :cap-style :join-style :fill-style
  653.        :fill-rule :tile :stipple :ts-x :ts-y :font :subwindow-mode
  654.        :exposures :clip-x :clip-y :clip-mask :dash-offset :dashes
  655.        :arc-mode))
  656.  
  657. (deftype event-key ()
  658.   '(member :key-press :key-release :button-press :button-release :motion-notify
  659.        :enter-notify :leave-notify :focus-in :focus-out :keymap-notify
  660.        :exposure :graphics-exposure :no-exposure :visibility-notify
  661.        :create-notify :destroy-notify :unmap-notify :map-notify :map-request
  662.        :reparent-notify :configure-notify :gravity-notify :resize-request
  663.        :configure-request :circulate-notify :circulate-request :property-notify
  664.        :selection-clear :selection-request :selection-notify
  665.        :colormap-notify :client-message :mapping-notify))
  666.  
  667. (deftype error-key ()
  668.   '(member :access :alloc :atom :colormap :cursor :drawable :font :gcontext :id-choice
  669.        :illegal-request :implementation :length :match :name :pixmap :value :window))
  670.  
  671. (deftype draw-direction ()
  672.   '(member :left-to-right :right-to-left))
  673.  
  674. (defconstant *boole-vector*
  675.          '#(#.boole-clr #.boole-and #.boole-andc2 #.boole-1
  676.         #.boole-andc1 #.boole-2 #.boole-xor #.boole-ior
  677.         #.boole-nor #.boole-eqv #.boole-c2 #.boole-orc2
  678.         #.boole-c1 #.boole-orc1 #.boole-nand #.boole-set))
  679.  
  680. (deftype boole-constant ()
  681.   `(member ,boole-clr ,boole-and ,boole-andc2 ,boole-1
  682.        ,boole-andc1 ,boole-2 ,boole-xor ,boole-ior
  683.        ,boole-nor ,boole-eqv ,boole-c2 ,boole-orc2
  684.        ,boole-c1 ,boole-orc1 ,boole-nand ,boole-set))
  685.  
  686. (defstruct visual-info
  687.   (id 0 :type card29)
  688.   (class :static-gray :type (member :static-gray :static-color :true-color
  689.                     :gray-scale :pseudo-color :direct-color))
  690.   (red-mask 0 :type pixel)
  691.   (green-mask 0 :type pixel)
  692.   (blue-mask 0 :type pixel)
  693.   (bits-per-rgb 1 :type card8)
  694.   (colormap-entries 0 :type card16)
  695.   (plist nil :type list)            ; Extension hook
  696.   )
  697.  
  698. (defstruct screen
  699.   (root nil :type (or null window))
  700.   (width 0 :type card16)
  701.   (height 0 :type card16)
  702.   (width-in-millimeters 0 :type card16)
  703.   (height-in-millimeters 0 :type card16)
  704.   (depths nil :type (alist (image-depth depth) ((list visual-info) visuals)))
  705.   (root-depth 1 :type image-depth)
  706.   (root-visual 0 :type card29)
  707.   (default-colormap nil :type (or null colormap))
  708.   (white-pixel 0 :type pixel)
  709.   (black-pixel 1 :type pixel)
  710.   (min-installed-maps 1 :type card16)
  711.   (max-installed-maps 1 :type card16)
  712.   (backing-stores :never :type (member :never :when-mapped :always))
  713.   (save-unders-p nil :type boolean)
  714.   (event-mask-at-open 0 :type mask32)
  715.   (plist nil :type list)            ; Extension hook
  716.   )
  717.  
  718. ;; The list contains alternating keywords and integers.
  719. (deftype font-props () 'list)
  720.  
  721. (defstruct font-info
  722.   (direction :left-to-right :type draw-direction)
  723.   (min-char 0 :type card16)   ;; First character in font
  724.   (max-char 0 :type card16)   ;; Last character in font
  725.   (min-byte1 0 :type card8)   ;; The following are for 16 bit fonts
  726.   (max-byte1 0 :type card8)   ;; and specify min&max values for
  727.   (min-byte2 0 :type card8)   ;; the two character bytes
  728.   (max-byte2 0 :type card8)
  729.   (all-chars-exist-p nil :type boolean)
  730.   (default-char 0 :type card16)
  731.   (min-bounds nil :type (or null vector))
  732.   (max-bounds nil :type (or null vector))
  733.   (ascent 0 :type int16)
  734.   (descent 0 :type int16)
  735.   (properties nil :type font-props))
  736.  
  737. (defstruct (font (:constructor make-font-internal))
  738.   (id-internal nil :type (or null resource-id)) ;; NIL when not opened
  739.   (display nil :type (or null display))
  740.   (reference-count 0 :type fixnum)
  741.   (name "" :type (or null string)) ;; NIL when ID is for a GContext
  742.   (font-info-internal nil :type (or null font-info))
  743.   (char-infos-internal nil :type (or null vector))
  744.   (local-only-p t :type boolean) ;; When T, always calculate text extents locally
  745.   (plist nil :type list)            ; Extension hook
  746.   )
  747.  
  748. (proclaim '(inline font-id font-font-info font-char-infos make-font))
  749.  
  750. (defun font-id (font)
  751.   ;; Get font-id, opening font if needed
  752.   (or (font-id-internal font)
  753.       (open-font-internal font)))
  754.  
  755. (defun font-font-info (font)
  756.   (or (font-font-info-internal font)
  757.       (query-font font)))
  758.  
  759. (defun font-char-infos (font)
  760.   (or (font-char-infos-internal font)
  761.       (progn (query-font font)
  762.          (font-char-infos-internal font))))
  763.  
  764. (defun make-font (&key id
  765.           display
  766.           (reference-count 0)
  767.           (name "")
  768.           (local-only-p t)
  769.           font-info-internal)
  770.   (make-font-internal :id-internal id
  771.               :display display
  772.               :reference-count reference-count
  773.               :name name
  774.               :local-only-p local-only-p
  775.               :font-info-internal font-info-internal))
  776.  
  777. ; For each component (<name> <unspec> :type <type>) of font-info,
  778. ; there is a corresponding function:
  779.  
  780. ;(defun font-<name> (font)
  781. ;  (declare (type font font)
  782. ;       (values <type>)))
  783.  
  784. (eval-when (eval compile) ;; I'd rather use macrolet, but Symbolics doesn't like it...
  785.  
  786. (defmacro make-font-info-accessors (useless-name &body fields)
  787.   `(within-definition (,useless-name make-font-info-accessors)
  788.      ,@(mapcan
  789.      #'(lambda (field)
  790.          (let* ((type (second field))
  791.             (n (string (first field)))
  792.             (name (xintern 'font- n))
  793.             (accessor (xintern 'font-info- n)))
  794.            `((proclaim '(inline ,name))
  795.          (defun ,name (font)
  796.            (declare (type font font))
  797.            (declare-values ,type)
  798.            (,accessor (font-font-info font))))))
  799.      fields)))
  800.  
  801. ) ;; End eval-when
  802.  
  803. (make-font-info-accessors ignore
  804.   (direction draw-direction)
  805.   (min-char card16)
  806.   (max-char card16)
  807.   (min-byte1 card8)
  808.   (max-byte1 card8)
  809.   (min-byte2 card8)
  810.   (max-byte2 card8)
  811.   (all-chars-exist-p boolean)
  812.   (default-char card16)
  813.   (min-bounds vector)
  814.   (max-bounds vector)
  815.   (ascent int16)
  816.   (descent int16)
  817.   (properties font-props))
  818.  
  819. (defun font-property (font name)
  820.   (declare (type font font)
  821.        (type keyword name))
  822.   (declare-values (or null int32))
  823.   (getf (font-properties font) name))
  824.  
  825. (eval-when (eval compile) ;; I'd rather use macrolet, but Symbolics doesn't like it...
  826.  
  827. (defmacro make-mumble-equal (type)
  828.   ;; When cached, EQ works fine, otherwise test resource id's and displays
  829.   (let ((predicate (xintern type '-equal))
  830.     (id (xintern type '-id))
  831.     (dpy (xintern type '-display)))
  832.     (if (member type *clx-cached-types*)
  833.     `(within-definition (,type make-mumble-equal)
  834.        (proclaim '(inline ,predicate))
  835.        (defun ,predicate (a b) (eq a b)))
  836.       `(within-definition (,type make-mumble-equal)
  837.      (defun ,predicate (a b)
  838.        (declare (type ,type a b))
  839.        (and (= (,id a) (,id b))
  840.         (eq (,dpy a) (,dpy b))))))))
  841.  
  842. ) ;; End eval-when
  843.  
  844. (make-mumble-equal window)
  845. (make-mumble-equal pixmap)
  846. (make-mumble-equal cursor)
  847. (make-mumble-equal font)
  848. (make-mumble-equal gcontext)
  849. (make-mumble-equal colormap)
  850. (make-mumble-equal drawable)
  851.  
  852. ;;;
  853. ;;; Event-mask encode/decode functions
  854. ;;;    Converts from keyword-lists to integer and back
  855. ;;;
  856. (defun encode-mask (key-vector key-list key-type)
  857.   ;; KEY-VECTOR is a vector containg bit-position keywords.  The position of the
  858.   ;; keyword in the vector indicates its bit position in the resulting mask
  859.   ;; KEY-LIST is either a mask or a list of KEY-TYPE
  860.   ;; Returns NIL when KEY-LIST is not a list or mask.
  861.   (declare (type vector key-vector)
  862.        (type (or mask32 list) key-list))
  863.   (declare-values (or mask32 nil))
  864.   (typecase key-list
  865.     (mask32 key-list)
  866.     (list (let ((mask 0))
  867.         (dolist (key key-list mask)
  868.           (let ((bit (position key (the vector key-vector) :test #'eq)))
  869.         (unless bit
  870.           (x-type-error key key-type))
  871.         (setq mask (logior mask (ash 1 bit)))))))))
  872.  
  873. (defun decode-mask (key-vector mask)
  874.   (declare (type (simple-array keyword (*)) key-vector)
  875.        (type mask32 mask))
  876.   (declare-values list)
  877.   (do ((m mask (ash m -1))
  878.        (bit 0 (1+ bit))
  879.        (len (length key-vector))
  880.        (result nil))       
  881.       ((or (zerop m) (>= bit len)) result)
  882.     (declare (type mask32 m)
  883.          (fixnum bit len)
  884.          (list result))
  885.     (when (oddp m)
  886.       (push (aref key-vector bit) result))))
  887.  
  888. (defun encode-event-mask (event-mask)
  889.   (declare (type event-mask event-mask))
  890.   (declare-values mask32)
  891.   (or (encode-mask *event-mask-vector* event-mask 'event-mask-class)
  892.       (x-type-error event-mask 'event-mask)))
  893.  
  894. (defun make-event-mask (&rest keys)
  895.   ;; This is only defined for core events.
  896.   ;; Useful for constructing event-mask, pointer-event-mask, device-event-mask.
  897.   (declare (type list keys)) ;; (list event-mask-class)
  898.   (declare-values mask32)
  899.   (encode-mask *event-mask-vector* keys 'event-mask-class))
  900.  
  901. (defun make-event-keys (event-mask)
  902.   ;; This is only defined for core events.
  903.   (declare (type mask32 event-mask))
  904.   (declare-values (list event-mask-class))
  905.   (decode-mask *event-mask-vector* event-mask))
  906.  
  907. (defun encode-device-event-mask (device-event-mask)
  908.   (declare (type device-event-mask device-event-mask))
  909.   (declare-values mask32)
  910.   (or (encode-mask *device-event-mask-vector* device-event-mask
  911.            'device-event-mask-class)
  912.       (x-type-error device-event-mask 'device-event-mask)))
  913.  
  914. (defun encode-modifier-mask (modifier-mask)
  915.   (declare (type modifier-mask modifier-mask)) ;; (list state-mask-key)
  916.   (declare-values mask16)
  917.   (or (encode-mask *state-mask-vector* modifier-mask 'modifier-key)
  918.       (and (eq modifier-mask :any) #x8000)
  919.       (x-type-error modifier-mask 'modifier-mask)))
  920.  
  921. (defun encode-state-mask (state-mask)
  922.   (declare (type (or mask16 list) state-mask)) ;; (list state-mask-key)
  923.   (declare-values mask16)
  924.   (or (encode-mask *state-mask-vector* state-mask 'state-mask-key)
  925.       (x-type-error state-mask '(or mask16 (list state-mask-key)))))
  926.  
  927. (defun make-state-mask (&rest keys)
  928.   ;; Useful for constructing modifier-mask, state-mask.
  929.   (declare (type list keys)) ;; (list state-mask-key)
  930.   (declare-values mask16)
  931.   (encode-mask *state-mask-vector* keys 'state-mask-key))
  932.  
  933. (defun make-state-keys (state-mask)
  934.   (declare (type mask16 state-mask))
  935.   (declare-values (list state-mask-key))
  936.   (decode-mask *state-mask-vector* state-mask))
  937.  
  938. (defun encode-pointer-event-mask (pointer-event-mask)
  939.   (declare (type pointer-event-mask pointer-event-mask))
  940.   (declare-values mask32)
  941.   (or (encode-mask *pointer-event-mask-vector* pointer-event-mask
  942.            'pointer-event-mask-class)
  943.       (x-type-error pointer-event-mask 'pointer-event-mask)))
  944.